<template>
	<view class="j-setting">
		<view class="j-ul">
			<view class="j-ul-row" @click="toMeInfo">
				<view class="j-ul-row-title">个人信息</view>
				<view class="j-ul-row-value">
					<view class="j-ul-row-v"><!-- <text class="j-ul-row-text">V1.0.0</text> --></view>
					<view class="hxf hxf-youbian"></view>
				</view>
			</view>
			<view class="j-ul-row" @click="toMeAddress">
				<view class="j-ul-row-title">收货地址</view>
				<view class="j-ul-row-value">
					<view class="j-ul-row-v"><!-- <text class="j-ul-row-text">V1.0.0</text> --></view>
					<view class="hxf hxf-youbian"></view>
				</view>
			</view>
			<view class="j-ul-row" @click="toMeSetPassword">
				<view class="j-ul-row-title">支付密码</view>
				<view class="j-ul-row-value">
					<view class="j-ul-row-v"><!-- <text class="j-ul-row-text">V1.0.0</text> --></view>
					<view class="hxf hxf-youbian"></view>
				</view>
			</view>
			<view class="j-ul-row" @click="toMeSetLoginPassword">
				<view class="j-ul-row-title">登录密码</view>
				<view class="j-ul-row-value">
					<view class="j-ul-row-v"><!-- <text class="j-ul-row-text">V1.0.0</text> --></view>
					<view class="hxf hxf-youbian"></view>
				</view>
			</view>
		</view>

		<view class="j-ul">
			<view class="j-ul-row" @click="toMeAbout">
				<view class="j-ul-row-title">关于我们</view>
				<view class="j-ul-row-value">
					<view class="j-ul-row-v"><!-- <text class="j-ul-row-text">V1.0.0</text> --></view>
					<view class="hxf hxf-youbian"></view>
				</view>
			</view>
			<view class="j-ul-row" @click="postFindNewVersionInfo">
				<view class="j-ul-row-title">检查更新</view>
				<view class="j-ul-row-value">
					<view class="j-ul-row-v">
						<text class="j-ul-row-text">{{ nowVersion }}</text>
					</view>
					<view class="hxf hxf-youbian"></view>
				</view>
			</view>
		</view>

		<view class="j-ul-btn" v-if="isShowLogoutBtn"><u-button @click="showLogout">退出登录</u-button></view>

		<u-modal v-model="isShowLogout" ref="logoutModel" :show-cancel-button="true" @confirm="postLogout" content="确定退出登录？" :async-close="true"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			isShowLogoutBtn: false,
			isShowLogout: false,
			nowVersion: ''
		};
	},
	onLoad() {
		// 当前APP版本
		this.nowVersion = 'v' + plus.runtime.version;
		// 判断显示是否显示退出登录的按钮
		this.showLogoutBtn();
	},
	methods: {
		// 跳转个人信息
		toMeInfo(){
			this.$u.api
				.postIsLogin()
				.then(res => {
					console.log(res);
					uni.navigateTo({
						url: './me-info/me-info'
					});
				})
				.catch(err => {
					console.log(err);
					// if (err.status == 401) {
					// 	uni.navigateTo({
					// 		url:'../../login'
					// 	})
					// 	return false;
					// }
				});
		},
		// 跳转收货地址页面
		toMeAddress(){
			this.$u.api
				.postIsLogin()
				.then(res => {
					console.log(res);
					uni.navigateTo({
						url: './address/address'
					});
				})
				.catch(err => {
					console.log(err);
					// if (err.status == 401) {
					// 	uni.navigateTo({
					// 		url:'../../login'
					// 	})
					// 	return false;
					// }
				});
		},
		// 跳转设置支付密码页面
		toMeSetPassword(){
			this.$u.api
				.postIsLogin()
				.then(res => {
					console.log(res);
					uni.navigateTo({
						url: './pay-password/pay-password'
					});
				})
				.catch(err => {
					console.log(err);
					// if (err.status == 401) {
					// 	uni.navigateTo({
					// 		url:'../../login'
					// 	})
					// 	return false;
					// }
				});
		},
		// 跳转设置登录密码页面
		toMeSetLoginPassword(){
			this.$u.api
				.postIsLogin()
				.then(res => {
					console.log(res);
					uni.navigateTo({
						url: './login-password/login-edit-password'
					});
				})
				.catch(err => {
					console.log(err);
					// if (err.status == 401) {
					// 	uni.navigateTo({
					// 		url:'../../login'
					// 	})
					// 	return false;
					// }
				});
		},
		// 跳转关于我们
		toMeAbout(){
			uni.navigateTo({
				url: './about/about'
			});
		},
		// 是否显示退出登录的按钮
		showLogoutBtn(){
			this.$u.api
				.postIsLogin()
				.then(res => {
					console.log(res);
					this.isShowLogoutBtn = true;
				})
				.catch(err => {
					console.log(err);
					if (err.status == 401) {
						this.isShowLogoutBtn = false;
						return false;
					}
				});
		},
		// 显示退出登录提示框
		showLogout() {
			this.isShowLogout = true;
		},
		// 退出登录
		postLogout() {
			this.$u.api
				.postLogout()
				.then(res => {
					console.log(res);
					this.$refs.uToast.show({
						title: '退出成功'
					});
					setTimeout(() => {
						this.isShowLogout = false;
						uni.navigateBack();
					}, 300);
				})
				.catch(err => {
					console.log(err);
					this.$refs.logoutModel.clearLoading();
				});
		},
		// 检查更新
		postFindNewVersionInfo() {
			this.$u.api
				.postFindNewVersionInfo({
					terminalType: 'Android'
				})
				.then(res => {
					console.log(res, '[设置页面，检查版本更新接口]');

					if (res.data) {
						let nowVersion = plus.runtime.version.split('.').join('');
						let sysVersion = res.data.currentVersion.split('.').join('');

						if (Number(nowVersion) < Number(sysVersion)) {
							uni.navigateTo({
								url: '/pages/update'
							});
						} else {
							this.$refs.uToast.show({
								title: '已是最新版本'
							});
						}
					} else {
						this.$refs.uToast.show({
							title: '已是最新版本'
						});
					}
				})
				.catch(err => {
					console.log(err);
				});
		}
	}
};
</script>

<style>
page {
	background-color: #f3f3f3;
}
</style>
<style scoped lang="scss">
.j-ul {
	padding: 0 28rpx;
	margin-top: 20rpx;
	background-color: #ffffff;

	.j-ul-row {
		display: flex;
		height: 102rpx;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1rpx solid #f1f1f1;

		&:last-of-type {
			border: 1rpx solid rgba(0, 0, 0, 0);
		}

		.j-ul-row-title {
			color: #333333;
		}

		.j-ul-row-value {
			display: flex;
			align-items: center;

			.j-ul-row-v {
				.j-ul-row-text {
					color: #797979;
				}
			}

			.hxf-youbian {
				color: #797979;
				font-size: 40rpx;
				padding-top: 5rpx;
			}
		}
	}
}

.j-ul-btn {
	padding: 0 30rpx;
	margin-top: 50rpx;
}
</style>
